package com.zs.toolbox.web2j.db.config;

/**
 * @author shuai.zhang  on 2018/10/11
 * @description
 */
public class WtDbContextHolder {
    private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
    private static final ThreadLocal<Boolean> onlyRead = new ThreadLocal<>();


    public static void setDbType(String datasourceName) {
        contextHolder.set(datasourceName);
    }


    public static void setOnlyRead() {
        onlyRead.set(true);
    }

    public static void clearOnlyRead() {
        onlyRead.remove();
    }

    /**
     * 取得当前数据源
     *
     * @return
     */
    public static String getDbType() {
        if (onlyRead.get() != null) {
            return contextHolder.get() + "-slaver";
        } else {
            return contextHolder.get();
        }
    }

    /**
     * 清除上下文数据
     */
    public static void clearDbType() {
        contextHolder.remove();
    }
}
